import { BattleCard, Skill, SkillInfo, CardAction } from '../types';

export const recoverAction = (card: BattleCard, skl: SkillInfo, target: BattleCard[]) => {
  const action: CardAction = {
    from: card.from,
    to: target.map(v => v.from)[0],
    id: card.base.id,
    avatar: card.base.heroAvatar,
    skill: skl.skill.id,
    skillType: skl.type,
    angry: 0,
    target: [],
  };

  // 出手后怒气处理 -先清空，后补充
  if (skl.type === 2) {
    card.battle.ANGRY = 0;
  }

  // 恢复开始
  for (let i = 0; i < target.length; i++) {
    const v = target[i];
    // 计算恢复数值
    const recover = calcRecover(card, v, skl.skill);
    console.log(`恢复血量：${recover}`);
    // 恢复处理
    v.battle.HP += recover;
    action.target.push({
      from: v.from,
      id: v.base.id,
      action: skl.skill.action,
      buff: [],
      hit: 1,
      crit: 0,
      dmg: 0,
      recover: recover,
      die: 0,
      hp: v.battle.HP,
      angry: v.battle.ANGRY,
    });
  }

  card.battle.ANGRY = card.battle.ANGRY + 10 > 100 ? 100 : card.battle.ANGRY + 10;
  action.angry = card.battle.ANGRY;

  return action;
};

const calcRecover = (card: BattleCard, target: BattleCard, skl: Skill) => {
  const { ATK } = card.battle;
  const { ratio } = skl;

  const recoverNum = ATK * (ratio / 100);
  console.log(`恢复数值：${recoverNum}`);

  if (recoverNum + target.battle.HP > target.base.hp) {
    return target.base.hp - target.battle.HP;
  }

  return Math.round(recoverNum);
};
